package com.hsh;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;

/**
 * Handles a server-side channel.
 */
public class DiscardServerHandler extends ChannelInboundHandlerAdapter { // (1)


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //((ByteBuf) msg).release();
        final ByteBuf in = (ByteBuf) msg;
        //System.out.println(in.toString(io.netty.util.CharsetUtil.US_ASCII));
        try {
            while (in.isReadable()) {
                System.out.println((char) in.readByte());
            }
        } finally {
            ReferenceCountUtil.release(msg);
        }

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

        //Close the connection when an exception is raised;
        cause.printStackTrace();
        ctx.close();

    }
}